package com.llseven.ai.spring.llseven.service;


import com.llseven.ai.spring.llseven.entity.Task;
import com.llseven.ai.spring.llseven.util.DatabaseUtils;
import org.springframework.stereotype.Service;

import java.sql.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ywb
 * @date 2025-04-21 20:25
 */
@Service
public class TaskService {
    
    // 新增任务
    public void addTask(Task task) {
        String sql = "INSERT INTO tasks (task_id, content, base_url) VALUES (?, ?, ?)";
        try (Connection conn = DatabaseUtils.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, task.getTaskId());
            stmt.setString(2, task.getContent());
            stmt.setString(3, task.getBaseUrl());
            stmt.executeUpdate();
            System.out.println("Task added successfully.");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    // 查询所有任务
    public List<Task> getAllTasks() {
        List<Task> tasks = new ArrayList<>();
        String sql = "SELECT * FROM tasks";
        try (Connection conn = DatabaseUtils.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                LocalDateTime createdAt = rs.getTimestamp("created_at") != null
                    ? rs.getTimestamp("created_at").toLocalDateTime()
                    : null;
                
                LocalDateTime updatedAt = rs.getTimestamp("updated_at") != null
                    ? rs.getTimestamp("updated_at").toLocalDateTime()
                    : null;
                
                
                Task task = new Task(
                    rs.getInt("id"),
                    rs.getString("task_id"),
                    rs.getString("content"),
                    rs.getString("base_url"),
                    rs.getString("result_url"),
                    createdAt,
                    updatedAt
                );
                tasks.add(task);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return tasks;
    }
    
    // 根据 task_id 修改任务
    public void updateTaskByTaskId(Task task) {
        String sql = "UPDATE tasks SET result_url = ? WHERE task_id = ?";
        try (Connection conn = DatabaseUtils.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, task.getBaseUrl());
            stmt.setString(2, task.getTaskId());
            int rowsUpdated = stmt.executeUpdate();
            if (rowsUpdated > 0) {
                System.out.println("Task with task_id " + task.getTaskId() + " updated successfully.");
            } else {
                System.out.println("No task found with task_id " + task.getTaskId());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

